package com.itheima.reggie.common;

import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.pojo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * Created by ZhaoJun on 2021/11/22 8:56
 */
@Slf4j
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R ex1(SQLIntegrityConstraintViolationException ex){

        ex.printStackTrace();
        return R.error("该数据已存在");
    }
    @ExceptionHandler(CustomException.class)
    public R customEx(CustomException ex){
        log.info(ex.getMsg());
        return R.error(ex.getMsg());
    }
}
